﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.IO;
using System.Text;
using FTchina.Library;
namespace FTchina.OA
{
    /// <summary>
    ///OALogin 的摘要说明
    /// </summary>
    public class OALogin
    {
        public OALogin()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }
        public OALogin(string uid, string upsw)
        {
            UID = uid;
            UPsw = upsw;
        }
        /// <summary>
        /// 登录用户名
        /// </summary>
        public String UID { get; set; }
        /// <summary>
        /// 登录密码
        /// </summary>
        public String UPsw { get; set; }

        /// <summary>
        /// 是否登录成功，通过模拟登录
        /// </summary>
        public Boolean IsLogin
        {
            get
            {
                string url = "http://www.sd.unicom.local/pkmslogin.form";
                string pstr = "userPortal=&username={0}&password={1}&login-form-type=pwd";
                string re = Post2Http(url, Encoding.UTF8, string.Format(pstr, UID, UPsw));
                return re.Contains("Moved Temporarily");//如果包含登录后页面的特定返回值，就认为登录成功。
            }
        }
        /// <summary>
        /// 通过sd.cnc.intra判断用户是否成功登陆
        /// </summary>
        public Boolean IsLoginByLdap
        {
            get
            {
                return LDAPHelper.CheckLDAPAccountADODB(UID, UPsw);
            }
        }
        private string Post2Http(string url, Encoding en, string poststr)
        {
            try
            {
                HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
                //taobaocc.SetCookies(new Uri(url), "PHPSESSID=ad991642306cca18f43bf2f91361d198");
                //request.CookieContainer = taobaocc;
                request.ContentType = "application/x-www-form-urlencoded";
                request.Method = "post";
                request.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/xaml+xml, application/x-ms-xbap, application/x-ms-application, */*";
                request.Referer = "http://www.sd.unicom.local:9080/inquiry/login11.jsp";

                request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727)";
                request.Headers.Set("Accept-Language", "zh-cn");
                //request.Connection = "Keep-Alive";
                request.AllowAutoRedirect = false;
                byte[] bytes = en.GetBytes(poststr);
                request.ContentLength = bytes.Length;
                Stream requestStream = request.GetRequestStream();
                requestStream.Write(bytes, 0, bytes.Length);
                requestStream.Close();
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                StreamReader reader = new StreamReader(response.GetResponseStream(), en);
                string str = reader.ReadToEnd();
                reader.Close();
                return str;
            }
            catch (SystemException er)
            {
                LogHelper.Error(er.Message);
                return "";
            }
        }
    }
}